package org.st.js4nk;

import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.TextFormat;

import java.io.File;

public class Main extends PluginBase {
    @Override
    public void onEnable() {
        saveDefaultConfig();
//        saveResource("events/join.js");
//        saveResource("js/js.js");
        File jsdir = new File(getDataFolder() + "/js");
        File eventsdir = new File(getDataFolder() + "/events");
        if (!jsdir.exists()) {
            jsdir.mkdir();
        }
        if (!eventsdir.exists()) {
            eventsdir.mkdir();
        }
        loadeventlisteners(new File(getDataFolder(), "/events"));
        getServer().getPluginManager().registerEvents(new JSEventListener(), this);
        getLogger().info(TextFormat.GOLD + "JS4NK by:luo123 已加载");


    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equals("js")) {
            if (args.length > 0) {
                String js = args[0];
                File jsfile = new File(getDataFolder() + "/js/", js);
                JSRunner jsRunner = new JSRunner(jsfile);
                jsRunner.setCommandSender(sender);
                jsRunner.setArgs(args);
                new Thread(jsRunner).start();
                return true;
            } else {
                return false;
            }
        }
        if (label.equals("jsm")) {
            if (args.length > 0) {
                switch (args[0]) {
                    case "list":
                        sender.sendMessage("====已加载的监听器脚本=====");
                        for (JSEvent jsEvent : JSEventListener.getEvetnlisteners()) {
                            sender.sendMessage(jsEvent.jsfile.getName());
                        }

                        break;
                    case "disable":
                        String disablefile = args[1];
                        for (JSEvent jsEvent : JSEventListener.getEvetnlisteners()) {
                            if (jsEvent.jsfile.getName().equals(disablefile)) { //遍历全部，如果文件名称相同就删掉
                                JSEventListener.getEvetnlisteners().remove(jsEvent);
                                break;
                            }
                        }
                        sender.sendMessage("成功禁用");
                        break;
                    case "enable":
                        String enablefile = args[1];
                        File jsfile = new File(getDataFolder() + "/events/", enablefile);
                        if (jsfile.isFile() && jsfile.exists()) {
                            JSEventListener.registerlistener(new JSEvent(jsfile));
                        } else sender.sendMessage("文件不存在");
                        sender.sendMessage("成功启用");
                        break;
                    case "reload":
                        JSEventListener.getEvetnlisteners().clear();
                        loadeventlisteners(new File(getDataFolder(), "/events"));
                        sender.sendMessage("重载成功");
                        break;

                }
                return true;
            } else {
                sender.sendMessage("/js <name> [args] --运行脚本\n/jsm list --列出已经加载的监听器 \n/jsm disable <name>  --禁用监听器脚本\n/jsm enable <name> --加载监听器脚本\n/jsm reload 重载所有脚本");
                return true;
            }
        }
        return false;
    }

    private void loadeventlisteners(File file) {
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isFile()) {
                        JSEvent jsEvent = new JSEvent(file2);
                        JSEventListener.registerlistener(jsEvent);
                        getLogger().info(file2.getName() + "已加载");
                    }
                }
            }
        }
    }
}
